#include "send_msg_handle.h"

u8 temp_buf[BUF_SIZE];
static void print_data_hex(uint8_t *data, u16 len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    
    printf(", len1: %d\n", len);
}

void logic_send_light_msg_second(u8 *buf,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc1 create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc2 create err\n");
        return;
    }

    bzero(uart_rx_buffer,length);
    spi_param->send_len = length; //待发送数据长度
    memcpy(uart_rx_buffer,buf,length);
    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    xQueueSend(spi2_queue_handle,&spi_param,0);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}



void logic_send_light_msg_ohter(u8 *buf,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length);
    spi_param->send_len = length; //待发送数据长度
    
    memcpy(uart_rx_buffer,buf,length);

    u8 buf2[LED_SECOND_NUMBER_NUM];
    memcpy(buf2,buf,LED_SECOND_NUMBER_NUM);
    logic_send_light_msg_second(buf2,LED_SECOND_NUMBER_NUM);

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    xQueueSend(spi_queue_handle,&spi_param,0);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}


void logic_send_light_msg(u8 *buf,u16 length)
{

    if(get_flag()== 1)
    {
        //print_data_hex(buf,length);
        memcpy(temp_buf,buf,length); //复制发送的数据用于熄灯渐变效果
        for (int i=0;i<length; i=i+3)
        {
            float brightnessFactor =((float)(get_brighness_level()+1)/(float)101);
            AdjustBrightness(brightnessFactor,&buf[i+0],&buf[i+1],&buf[i+2]);
        }
        spi_send_param *spi_param = NULL;
        u8 *uart_rx_buffer = NULL;
        spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length));

        if(uart_rx_buffer == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }

        bzero(uart_rx_buffer,length);
        spi_param->send_len = length; //待发送数据长度

        u8 buf2[LED_SECOND_NUMBER_NUM];
        memcpy(buf2,buf,LED_SECOND_NUMBER_NUM);
        logic_send_light_msg_second(buf2,LED_SECOND_NUMBER_NUM);

        memcpy(uart_rx_buffer,buf,length);
        spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
        xQueueSend(spi_queue_handle,&spi_param,0);
        uart_rx_buffer =NULL; //防止野指针
        spi_param = NULL;
    }
}